<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
	// 权限管理首页
    public function getIndex(Request $request){
        // dd(session('id'));
         if($request['name']){
            //搜索分页
            $res = DB::table('department')
            ->join('post','post.DepartmentId','=','department.id')
            ->where('PostName','like','%'.$request['name'].'%')
            ->select('post.PostId','DepartmentName','PostName','post.Remark')
            ->paginate(10);
        }else{
            //所有分页
           
        $res = DB::table('department')
        ->join('post','post.DepartmentId','=','department.id')
        ->select('post.PostId','DepartmentName','PostName','post.Remark')
        // ->get();
        ->paginate(10);

        }
       
        $info = $request -> all();

        return view('department/post/post',['res'=>$res,'info'=>$info]);
        //解析页面
        // dd($res);
        // return view('department/post/post',['res'=>$res]);

    } 
    
    //添加
    public function getAdd(){
        $res = DB::table('department')->get();
        // dd($res);
        return view('department/post/add',['res'=>$res]);

    }

    //执行添加
    public function postInsert(Request $request){
        //表单验证
        $this->validate($request, [
        'PostName' => 'required',
        'Remark' => 'required',
        ],[
        'PostName.required'=>'岗位名称不能为空',
        'Remark.required'=>'备注不能为空',
        ]);
        $res = $request->only('DepartmentId','PostName','Remark');
        // dd($res);
        //执行
        DB::table('post')->insert($res);
        return redirect('/department/post')->with('添加成功');
    }

    //编辑
    public function getEdit($id)
    {
        $res = DB::table('department')
        ->join('post','post.DepartmentId','=','department.id')
        ->where('post.PostId',$id)
        ->get();
        $depart = DB::table('department')->get();
        // dd($depart);
        return view('/department/post/edit',['res'=>$res,'depart'=>$depart]);
    }

    //执行修改
    public function postUpdate(Request $request)
    {
        $id = $request->all()['id'];
        // dd($id);
        $res = $request->only('DepartmentId','PostName','Remark');
        // dd($res);
        //执行修改
        DB::table('post')->where('PostId',$id)->update($res);
        return redirect('/department/post')->with('修改成功');

    }

    //删除
    public function getDel($id)
    {
        // dd($id);
        //执行删除
        DB::table('post')->where('PostId',$id)->delete();
        return redirect('/department/post')->with('删除成功');

    }
}
